package com.campus.client.controller;


import com.campus.pojos.domain.dto.ArticleDto;
import com.campus.pojos.domain.dto.ArticleQueryDto;
import com.campus.pojos.domain.dto.PageQueryDto;
import com.campus.pojos.domain.vo.ArticleVo;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 贴子相关接口
 */
@RestController
@RequestMapping("/client/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /**
     * 发布帖子
     */
    @PostMapping("/save")
    public Result save(@RequestBody ArticleDto dto){
        log.info("保存帖子{}", dto);
        articleService.saveArticle(dto);
        return Result.success();
    }

    /**
     * 获取帖子列表
     */
    @PostMapping("/list")
    public Result<List<ArticleVo>> list(@RequestBody ArticleQueryDto dto){
        log.info("查询帖子列表 条件{}", dto);
        return articleService.articleList(dto);
    }

    /**
     * 根据id获取帖子
     * @return
     */
    @GetMapping("/{id}")
    public Result<ArticleVo> getArticleById(@PathVariable Long id){
        return articleService.getByArticleId(id);
    }

    /**
     * 编辑帖子
     * @return
     */
    @PutMapping("")
    public Result edit(@RequestBody ArticleDto articleDto){
        log.info("编辑帖子,{}",articleDto);
        articleService.editArticle(articleDto);
        return Result.success();
    }

    /**
     * 帖子点赞
     * @param articleId
     * @return
     */
    @PutMapping("/like")
    public Result like(Long articleId, Long userId){
        log.info("帖子点赞id:{}, 用户id:{}",articleId,userId);
        articleService.likeArticle(articleId, userId);
        return Result.success();
    }


    /**
     * 取消帖子点赞
     * @param articleId
     * @return
     */
    @PutMapping("/dislike")
    public Result dislike(Long articleId, Long userId){
        log.info("帖子点赞id:{}, 用户id:{}",articleId,userId);
        articleService.dislikeArticle(articleId, userId);
        return Result.success();
    }
    /**
     * 根据id删除帖子
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        log.info("根据id删除帖子 id{}",id);
        articleService.removeById(id);
        return Result.success();
    }

    /**
     * 获取有图片的帖子
     */
    @PostMapping("/photoArt")
    public Result<PageResult> getPhotoArticle(@RequestBody PageQueryDto dto){
        return articleService.getPhotoArticle(dto);
    }
}
